package com.kewill.jira.util;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.alibaba.druid.pool.DruidPooledConnection;
import com.kewill.jira.common.JiraAttribute;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.LinkedList;

/**
 * Created by YanJun on 2016/7/1.
 */
public class DbConnectionUtil {
    private static DbConnectionUtil connectionPool = null;
    private static DruidDataSource dataSource = null;
    private DbConnectionUtil(){}
    static{
        try {
            dataSource = (DruidDataSource)DruidDataSourceFactory
                    .createDataSource(PropertyUtil.loadProperty("jdbc.properties"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static DbConnectionUtil getInstance() {
        if(null == connectionPool){
            connectionPool = new DbConnectionUtil();
        }
        return connectionPool;
    }
    public DruidPooledConnection getConnection(){
        try {
            return dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    public void releaseConnection(DruidPooledConnection connection) {
        try {
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
